import Vue from 'vue'
import Vuex from 'vuex'
import $http from '@/main.js'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 存储 id
    id: '',
    // 存储 token
    token: ''

  },
  mutations: {
    // 存储 id
    setID (state, id) {
      state.id = id
      console.log(state.id)
    },
    // 存储 token
    setToken (state, token) {
      state.token = token
      console.log(state.token)
    }
  },
  actions: {
    async setUserID ({ commit }) {
      const { data: res } = await $http.post('index/index/setUserClient')
      commit('setID', res.data.client_id)
    },
    async setUserToken ({ commit }, data) {
      const { data: res } = await $http.post('index/user/login', data)
      commit('setToken', res.data.token)
    }
  },
  modules: {
  },
  plugins: [ // 默认存储所有state数据到localstorage
    createPersistedState()
  ]
})
// const store = new Vuex.Store({
//   // ...
//   plugins: [createPersistedState()]
// })
